package com.dzu.medium;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;

/**
 * @author ZhaoDong
 * @date 2022/12/23 11:09
 * @description 71. 简化路径
 */
public class Test71 {

    public static void main(String[] args) {
        System.out.println(simplifyPath("/a/./b/../../c/"));
    }

    public static String simplifyPath(String path) {

        List<String> list = Arrays.asList("", ".", "..");
        Deque<String> deque = new ArrayDeque<>();

        String[] split = path.split("/");
        for (String s : split) {
            if (!list.contains(s)){
                deque.add(s);
            }else if (!deque.isEmpty() && s.equals("..")){
                deque.pollLast();
            }
        }
        return "/" + String.join("/", deque);
    }
}
